package jumpingnotes.dao.hibernate;

import jumpingnotes.dao.GroupMemberDao;
import jumpingnotes.model.entity.GroupMember;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class GroupMemberHibernateDao extends GenericHibernateDao<GroupMember> implements GroupMemberDao
{
	public GroupMemberHibernateDao()
	{
		super(GroupMember.class);
	}

	@SuppressWarnings("unchecked")
	public boolean isMemberInGroup(final int memberId, final int groupId)
	{
		return (Boolean) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session
						.createQuery("select distinct gm from GroupMember gm where gm.member.memberId=:memberId and gm.group.groupId=:groupId");
				query.setInteger("memberId", memberId);
				query.setInteger("groupId", groupId);
				return (query.list().size() > 0);
			}
		});
	}
}
